package com.txtsqlclient.domain;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.txtsqlclient.dataaccess.database.connection.ConnectionException;

/**
 * @author spadda
 * 
 */
public class Database extends DataSource {

	/*
	 * Public access without getters/setters are explicitly written to avoid lot
	 * of redundant, unused code. When the need arises, this shall be refactored
	 * to provided getters and setters.
	 */

	protected String ip;

	protected String database;

	protected String user;

	protected String password;

	private static Log LOGGER = LogFactory.getLog(Database.class);

	/**
	 * String representation of this object
	 * 
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		StringBuffer buff = new StringBuffer();
		buff.append("[database type=").append(this.dsType).append(", ip=")
				.append(this.ip).append(", database=").append(this.database)
				.append(", user=").append(this.user).append("]");
		return buff.toString();
	}

	public Database(final DataSourceType dsType) {
		setDsType(dsType);
	}

	public Database(final DataSourceType dsType, final String ip,
			final String database, final String user, final String password) {
		this(dsType);
		this.ip = ip;
		this.database = database;
		this.user = user;
		this.password = password;
	}

	private void setDsType(final DataSourceType dsType) {
		if (DataSourceFactory.LEGAL_DATABASE_TYPES.contains(dsType)) {
			this.dsType = dsType;
		} else {
			throw new IllegalArgumentException("The given Data Source Type, :"
					+ dsType + " is not a valid Database.");
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.txtsqlclient.domain.DataSource#openConnection()
	 */
	@Override
	public Connection openConnection() throws ConnectionException {
		final String url = this.dsType.getConnectionURL(this);
		Connection dbCon = null;
		this.dsType.loadDriverClass();
		try {
			dbCon = DriverManager.getConnection(url, this.user, this.password);
		} catch (SQLException sqle) {
			throw new ConnectionException(
					"Error opening connection to the database:" + this, sqle);
		}
		LOGGER.info("Opened connection to the database");
		return dbCon;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.txtsqlclient.domain.DataSource#readConnectionDetailsFromFile()
	 */
	@Override
	public void readConnectionDetailsFromFile() {
		throw new UnsupportedOperationException();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.txtsqlclient.domain.DataSource#writeConnectionDetailsToFile()
	 */
	@Override
	public void writeConnectionDetailsToFile() {
		throw new UnsupportedOperationException();

	}

	public String formatURL(final String formatURLStr) {
		String urlStr = formatURLStr.replaceAll("\\{ip\\}", this.ip);
		urlStr = urlStr.replaceAll("\\{database\\}", this.database);
		LOGGER.debug("Formatted database access URL is: " + urlStr);
		return urlStr;
	}

	public int hashCode() {
		final int prime = 31;
		int hashCode = 7;
		hashCode = prime * hashCode + this.dsType.hashCode();
		hashCode = prime * hashCode + this.ip.hashCode();
		hashCode = prime * hashCode + this.user.hashCode();
		hashCode = prime * hashCode + this.database.hashCode();
		return hashCode;
	}

	public boolean equals(final Object ds) {
		if (ds instanceof Database) {
			Database db = (Database) ds;
			if (this.dsType.equals(db.dsType) && this.ip.equals(db.ip)
					&& this.database.equals(db.database)
					&& this.user.equals(db.user)) {
				return true;
			}
		}
		return false;
	}
}
